![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@fanoutio/pubcontrol
Advanced tools
Authors: Katsuyuki Ohmuro harmony7@pex2.jp, Konstantin Bokarius kon@fanout.io
An EPCP library for NodeJS
HTTP Extensible Pubsub Control Protocol (EPCP) defines a generalized and extensible data publishing protocol using HTTP. Data is published by way of HTTP POST, whose content is the JSON string representation of an object that follows certain structural guidelines.
Each message consists of one or more data items, and is always sent to a specified channel. The messages are associated with that channel and will only be delivered to those listeners subscribed to the channel.
This library contains a Format base class and a PubControl class that is used to send messages. This library also supports the case when the EPCP endpoint requires certain types of authentication.
npm install @fanoutio/pubcontrol
This example illustrates the process of instantiating the PubControl publisher class, defining a data format, and then publishing some data.
import PubControl from '@fanoutio/pubcontrol';
const { Format, Item } = PubControl;
// Instantiate PubControl publisher.
const pub = new PubControl({
'uri': 'http://www.example.com/path/to/endpoint'
});
// Define a data format.
class HttpResponseFormat extends Format {
content;
constructor(contentcontent) {
super();
this.content = content;
}
name() {
return 'http-response';
}
export() {
return { content: this.content, };
}
}
// Publish across all configured endpoints.
const item = new Item(new HttpResponseFormat('Test Publish!'));
pub.publish('my-channel', item)
.then(() => {
console.log('Publish successful!');
})
.catch(({message, context}) => {
console.log('Publish failed!');
console.log('Message: ' + message);
console.log('Context: ');
console.dir(context);
});
PubControl offers several ways of configuring endpoints. The simplest way
is by specifying an object with a uri
and optional iss
and key
fields.
const pub = new PubControl({
'uri': 'https://api.fanout.io/realm/<myrealm>',
'iss': '<myrealm>',
'key': Buffer.from('<myrealmkey>', 'base64'),
});
An advanced usage is to create an instance of a PubControlClient
and add it to the
PubControl instance yourself. This allows for finer tuning of authentication used
by the client.
import PubControl from '@fanoutio/pubcontrol';
const { PubControlClient } = PubControl;
const pub = new PubControl();
// Define the endpoint:
const pubclient = new PubControlClient('<myendpoint_uri>');
// Optionally set JWT auth: pubclient.setAuthJwt(<claim>, '<key>');
// Optionally set basic auth: pubclient.setAuthBasic('<user>', '<password>');
pub.addClient(pubclient);
Additionally, multiple endpoints can be included in a single configuration.
// Initialize PubControl with a single endpoint:
const pub = new PubControl();
// Add new endpoints by applying an endpoint configuration:
pub.applyConfig([
{'uri': '<myendpoint_uri_1>'},
{'uri': '<myendpoint_uri_2>'},
]);
If you ever need to clear all configured endpoints, call the removeAllClients
function.
// Remove all configured endpoints:
pub.removeAllClients();
In some cases, the EPCP endpoint requires authentication before allowing its use. This library can provide Basic and JWT authentication for these cases.
To use Basic authentication, create an instance of the PubControlClient
class,
use setBasicAuth()
to set the username and password, and add the instance to
the PubControl
instance via addClient()
as shown in the example above.
To use JWT authentication, pass a configuration to PubControl
when instantiating
it or via applyConfig()
and provide the claim as shown in the example above.
If the claim does not contain an exp
value, then this library will create an
appropriate value for that field on each use. Since the header is generated
from the authorization object each time it needs to be used, the library is
able to generate a new authorization header, even from the same auth object.
It is also possible to use a literal JWT string for JWT authentication. This may be useful in certain cases, such as when you are performing a push request on behalf of another service. That service can pre-encode the JWT token and hand it to you in its string representation. This way, that service does not need to hand the JWT signing key to you.
pubClient.setAuthJwt('######.######.######'); // Literal JWT string
The tests in this package require Node.js 12 or newer to run. This is because the tests run against the source JavaScript modules directly, which is a feature that was supported starting with Node.js 12.
(The runtime will work with lower versions of node and with browsers since they go through a transpile step.)
The CommonJS version of this package requires Node v8 or newer.
Require in your JavaScript:
const PubControl = require('@fanoutio/pubcontrol');
const { Format, Item } = PubControl;
const pub = new PubControl({uri: "<endpoint_uri>"});
If you are building a bundle, you may also import in your JavaScript.
import PubControl from '@fanoutio/pubcontrol';
const { Format, Item } = PubControl;
const pub = new PubControl({uri: "<endpoint_uri>"});
This package comes with full TypeScript type definitions, so you may use it with TypeScript as well.
import PubControl, { IFormat } from '@fanoutio/pubcontrol';
const { Format, Item } = PubControl;
const pub = new PubControl({uri: "<endpoint_uri>"});
// IFormat is a type declaration.
A build for browsers is available as the /browser/pubcontrol.js
file in the npm package,
or by running npm run build-browser
to build it. This may be included as a normal <script>
tag on your web page:
<script src="./node_modules/@fanoutio/pubcontrol/browser/pubcontrol.js"></script>
Of course, you may copy the file into your project and point to it instead.
In this usage, PubControl
is introduced to the global namespace. In addition,
Buffer
is exposed through a property on the PubControl
object.
const pub = new PubControl({uri: "<endpoint_uri>"});
const { Format, Item, Buffer, } = PubControl;
Included in this package is a demo that publishes a message using EPCP to the open-source Pushpin (https://pushpin.org/) server.
To run the demo:
npm install
npm run build-commonjs
Install Pushpin (see https://pushpin.org/docs/install/)
Make sure Pushpin has the default test config.
The routes
file should look like this:
* test
pushpin
curl http://localhost:7999/stream
node demo test "Message"
For more examples, including running pubcontrol in a browser,
see the js-pubcontrol-demos
repository.
(C) 2015, 2020 Fanout, Inc.
Licensed under the MIT License, see file COPYING for details.
FAQs
EPCP Library
We found that @fanoutio/pubcontrol demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.